﻿using LoveNote.Application.Share.Enums;
using LoveNote.Application.Share.Login;
using LoveNote.Database.Entities;
using LoveNote.Database.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using TookKit.Mvvm.Attributes;
using TookKit.Mvvm.Bindings;
using TookKit.Mvvm.Enums;

namespace LoveNote.Application.Work.ViewModels
{
    [DependencyInjection(Lifetime.Singleton)]
    internal class WorkViewModel : ObservableObject
    {
        private List<WorkEntity> works;
        public List<WorkEntity> Works
        {
            get { return works; }
            set { SetProperty(ref works, value); }
        }

        private WorkEntity work;
        public WorkEntity Work
        {
            get { return work; }
            set { SetProperty(ref work, value); }
        }

        public ICommand LoadedCommand { get; }
        public ICommand UnloadedCommand { get; }
        public ICommand CreateWorkCommand { get; }
        public ICommand DeleteWorkCommand { get; }
        public ICommand CompleteWorkCommand { get; }
        public ICommand SaveWorkCommand { get; }

        private IWorkRepository WorkRepository { get; }
        public ISession Session { get; }


        public WorkViewModel(ISession session, IWorkRepository workRepository)
        {
            Session = session;
            WorkRepository = workRepository;
            LoadedCommand = new RelayCommand(OnLoadedCommand);
            UnloadedCommand = new RelayCommand(OnUnloadedCommand);
            CreateWorkCommand = new RelayCommand(OnCreateWorkCommand);
            DeleteWorkCommand = new RelayCommand<WorkEntity>(OnDeleteWorkCommand);
            CompleteWorkCommand = new RelayCommand<WorkEntity>(OnCompleteWorkCommand);
            SaveWorkCommand = new RelayCommand(OnSaveWorkCommand);
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="entity"></param>
        private void OnSaveWorkCommand()
        {
            WorkRepository.SaveChanged();
        }

        private void OnCompleteWorkCommand(WorkEntity entity)
        {
            if (entity != null)
            {
                entity.Complete = (int)CompleteType.已完成;
                entity.CompleteDate = DateTime.Now;
            }            
        }       

        /// <summary>
        /// 删除工作计划
        /// </summary>
        /// <param name="obj"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void OnDeleteWorkCommand(WorkEntity entity)
        {
            var result = Session.Show("是否执行当前操作？", System.Windows.MessageBoxButton.YesNo);
            if (result == null || result.Value == false)
                return;
            int count = WorkRepository.Delete(entity);
            if (count > 0)
            {
                LoadWorks();
            }
        }

        private void OnLoadedCommand()
        {
            LoadWorks();
        }        

        /// <summary>
        /// 保存当前数据
        /// </summary>
        private void OnUnloadedCommand()
        {
            WorkRepository.SaveChanged();
        }

        private void OnCreateWorkCommand()
        {
            CreateWork();
            LoadWorks();
        }

        /// <summary>
        /// 创建当前用户的新工作计划
        /// </summary>
        private void CreateWork()
        {
            var entity = new WorkEntity();
            entity.InsertDate = DateTime.Now;
            entity.UserId = Session.CurrentUser.Id;
            entity.Title = "请在此输入标题";
            entity.Content = "请在此输入内容...";
            int count = WorkRepository.Insert(entity);
            if (count > 0)
            {
                LoadWorks();
                Work = entity;
            }
        }

        /// <summary>
        /// 加载当前用户的所有工作计划
        /// </summary>
        private void LoadWorks()
        {
            Works = WorkRepository.GetAll()
                .FindAll(t => t.UserId == Session.CurrentUser.Id)
                .OrderByDescending(t => t.InsertDate)
                .ToList();
        }
    }
}
